#!/usr/bin/env python3
#-*-encoding:utf-8-*-

#coding=utf-8
import subprocess
# Altis 阿尔蒂斯 ${currVersion}
# Enoch 利沃尼亚 
# Malden  马尔登2035
# SefrouRamal  塞夫鲁拉马尔
# Stratis 斯特拉蒂斯
# Tanoa  塔诺亚
# 部署的时候修改_new_str版本号即可 编译命令 python .\complie.py
_new_str="Rambo升级版v20220217"
_old_str="${currVersion}"


def alter(file,old_str,new_str):
    """
    替换文件中的字符串
    :param file:文件名
    :param old_str:就字符串
    :param new_str:新字符串
    :return:
    """
    file_data = ""
    with open(file, "r", encoding="utf-8") as f:
        for line in f:
            if old_str in line:
                line = line.replace(old_str,new_str)
            file_data += line
    with open(file,"w",encoding="utf-8") as f:
        f.write(file_data)

alter("E:/Users/lambert/git/co10_Escape/Missions/Altis/include/defines.hpp", _old_str, _new_str)
alter("E:/Users/lambert/git/co10_Escape/Missions/Enoch/include/defines.hpp", _old_str, _new_str)
alter("E:/Users/lambert/git/co10_Escape/Missions/Malden/include/defines.hpp", _old_str, _new_str)
alter("E:/Users/lambert/git/co10_Escape/Missions/SefrouRamal/include/defines.hpp", _old_str, _new_str)
alter("E:/Users/lambert/git/co10_Escape/Missions/Stratis/include/defines.hpp", _old_str, _new_str)
alter("E:/Users/lambert/git/co10_Escape/Missions/Tanoa/include/defines.hpp", _old_str, _new_str)
alter("E:/Users/lambert/git/co10_Escape/package.bat", _old_str, _new_str)

def cmd_test():
    # cmd = 'cmd.exe d:/start.bat'
    p = subprocess.Popen("cmd.exe /c" + "E:/Users/lambert/git/co10_Escape/package.bat", stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    curline = p.stdout.readline()
    while (curline != b''):
        print(curline)
        curline = p.stdout.readline()
    p.wait()
    print(p.returncode)

cmd_test()

alter("E:/Users/lambert/git/co10_Escape/Missions/Altis/include/defines.hpp", _new_str, _old_str)
alter("E:/Users/lambert/git/co10_Escape/Missions/Enoch/include/defines.hpp", _new_str, _old_str)
alter("E:/Users/lambert/git/co10_Escape/Missions/Malden/include/defines.hpp", _new_str, _old_str)
alter("E:/Users/lambert/git/co10_Escape/Missions/SefrouRamal/include/defines.hpp", _new_str, _old_str)
alter("E:/Users/lambert/git/co10_Escape/Missions/Stratis/include/defines.hpp", _new_str, _old_str)
alter("E:/Users/lambert/git/co10_Escape/Missions/Tanoa/include/defines.hpp", _new_str, _old_str)
alter("E:/Users/lambert/git/co10_Escape/package.bat", _new_str, _old_str)
